import React, { useState, useEffect } from 'react';
import { defaultAxios } from '@/request/request';
import { useAddKey, timestampToTime } from '@/hooks/useAddKey';
export function tab(Com: any, params: any) {
  return () => {
    const [arr, setArr] = useState<any>([]);
    const [total, setTotal] = useState<any>(1);
    function getData() {
      return defaultAxios.get(params).then((res: any) => {
        setArr(useAddKey(res.data.arr));
        setTotal(res.data.count);
      });
    }
    const addkefu = (value: any) => {
      let addArr = JSON.parse(JSON.stringify(arr));
      console.log(addArr);
      addArr.push({
        kefu_id: addArr[addArr.length - 1].kefu_id + 1,
        kefu_code: 'kefu5df785ee07ae1',
        kefu_name: value.kefu_name,
        kefu_avatar: '/static/common/images/kefu.jpg',
        kefu_password: 'E10ADC3949BA59ABBE56E057F20F883E',
        online_status: 1,
        create_time: timestampToTime(Math.random().toLocaleString()),
        update_time: '2023-02-26 18:54:19',
        client_id: '',
      });
      setArr(addArr);
    };
    const change = (id: number, name: string) => {
      let editArr = JSON.parse(JSON.stringify(arr));
      useAddKey(editArr);
      editArr.forEach((item: any) => {
        if (item.kefu_id === id) {
          return (item.kefu_name = name);
        }
        setArr(editArr);
      });
    };
    useEffect(() => {
      getData();
    }, []);
    return (
      <div>
        <Com change={change} arr={arr} total={total} addkefu={addkefu}></Com>
      </div>
    );
  };
}

export default tab;
